package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.Person;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IPersonService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 基础人员Controller
 *
 * @author ess
 * @date 2021-10-14
 */
@Api("基础人员信息管理")
@RestController
@RequestMapping("/system/person")
public class PersonController extends BaseController {
    @Autowired
    private IPersonService personService;

    /**
     * 查询基础人员列表
     */
    @ApiOperation("查询基础人员列表")
    @PreAuthorize("@ss.hasPermi('system:person:list')")
    @GetMapping("/list")
    public TableDataInfo list(Person person) {
        startPage();
        List<Person> list = personService.selectPersonList(person);
        return getDataTable(list);
    }

//    /**
//     * 根据姓名查询基础人员列表
//     */
//    @ApiOperation("根据姓名查询基础人员列表 ")
//    @ApiImplicitParam(name = "personName", value = "基础人员姓名", required = true, dataType = "String", dataTypeClass = String.class)
//    @RequestMapping(value = "/listByPersonName")
//    public AjaxResult listByPersonName(@RequestBody String personName) {
//        AjaxResult ajax = AjaxResult.success();
//        List<Person> perosnList = personService.selectPersonListByPersonName(personName);
//        ajax.put("perosnList", perosnList);
//        return ajax;
//    }

    /**
     * 导出基础人员列表
     */
    @ApiOperation("导出基础人员列表")
    @PreAuthorize("@ss.hasPermi('system:person:export')")
    @Log(title = "基础人员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Person person) {
        List<Person> list = personService.selectPersonList(person);
        ExcelUtil<Person> util = new ExcelUtil<Person>(Person.class);
        return util.exportExcel(list, "基础人员数据");
    }

    /**
     * 获取基础人员详细信息
     */
    @ApiOperation("获取基础人员详细信息")
    @ApiImplicitParam(name = "personId", value = "基础人员ID", required = true, dataType = "String", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermi('system:person:query')")
    @GetMapping(value = "/{personId}")
    public AjaxResult getInfo(@PathVariable("personId") String personId) {
        return AjaxResult.success(personService.selectPersonByPersonId(personId));
    }

    /**
     * 新增基础人员
     */
    @ApiOperation("新增基础人员")
    @PreAuthorize("@ss.hasPermi('system:person:add')")
    @Log(title = "基础人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Person person) {

//        if (StringUtils.isNotEmpty(person.getEmpNum()) && UserConstants.NOT_UNIQUE.equals(personService.checkEmpNumUnique(person.getEmpNum()))) {
//            return AjaxResult.error("新增基础人员'" + person.getPersonName() + "'失败，人员编号已存在");
//        }

        if (person.getLoginPsw() != null) {
            person.setLoginPsw(SecurityUtils.encryptPassword(person.getLoginPsw()));
        }
        return toAjax(personService.insertPerson(person));
    }

    /**
     * 修改基础人员
     */
    @ApiOperation("修改基础人员")
    @PreAuthorize("@ss.hasPermi('system:person:edit')")
    @Log(title = "基础人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Person person) {


        if (StringUtils.isNotEmpty(person.getEmpNum())&& UserConstants.NOT_UNIQUE.equals(personService.checkEmpNumUnique(person.getEmpNum()))) {
            Person personOld = personService.selectPersonByPersonId(person.getPersonId());
            if (!personOld.getEmpNum() .equals(person.getEmpNum())) {
                return AjaxResult.error("修改基础人员'" + person.getPersonName() + "'失败，人员编号已存在");
            }
        }

//        if (person.getLoginPsw() != null) {
//            person.setLoginPsw(SecurityUtils.encryptPassword(person.getLoginPsw()));
//        }

        return toAjax(personService.updatePerson(person));
    }

    /**
     * 删除基础人员
     */
    @ApiOperation("删除基础人员")
    @PreAuthorize("@ss.hasPermi('system:person:remove')")
    @Log(title = "基础人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{personIds}")
    public AjaxResult remove(@PathVariable String[] personIds) {
        return toAjax(personService.deletePersonByPersonIds(personIds));
    }


    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('system:person:resetPwd')")
    @Log(title = "基础人员", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody Person person) {

        if (person.getPersonId().equals("000001")) {
//            throw new ServiceException("不允许操作超级管理员用户");
            return toAjax(0);
        }
        person.setLoginPsw(SecurityUtils.encryptPassword(person.getLoginPsw()));
        return toAjax(personService.resetPwd(person));
    }
}
